﻿/*
' Copyright (c) 2013  DotNetNuke Corporation
'  All rights reserved.
' 
' THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED
' TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
' THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF
' CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
' DEALINGS IN THE SOFTWARE.
' 
*/

using System;
using DotNetNuke.Entities.Users;
using DotNetNuke.Modules.mwOrgChart.Components;
using DotNetNuke.Services.Exceptions;

using DotNetNuke.Modules.mwOrgChart.Components.Business;
using DotNetNuke.Common.Utilities;

namespace DotNetNuke.Modules.mwOrgChart
{
    /// -----------------------------------------------------------------------------
    /// <summary>   
    /// The Edit class is used to manage content
    /// 
    /// Typically your edit control would be used to create new content, or edit existing content within your module.
    /// The ControlKey for this control is "Edit", and is defined in the manifest (.dnn) file.
    /// 
    /// Because the control inherits from mwOrgChartModuleBase you have access to any custom properties
    /// defined there, as well as properties from DNN such as PortalId, ModuleId, TabId, UserId and many more.
    /// 
    /// </summary>
    /// -----------------------------------------------------------------------------
    public partial class NodeRelations : mwOrgChartModuleBase
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            try
            {
                //Implement your edit logic for your module
                if (!Page.IsPostBack)
                {
                    //get a list of users to assign the user to the Object
                    ddlUsers.DataSource = UserController.GetUsers(PortalId);
                    ddlUsers.DataTextField = "Username";
                    ddlUsers.DataValueField = "UserId";
                    ddlUsers.DataBind();

                    // Namespace and collection call for DNN security model to get current user info and roles
                    DotNetNuke.Security.Roles.RoleController rcc = new DotNetNuke.Security.Roles.RoleController();
                    DotNetNuke.Entities.Users.UserInfo userInfo = DotNetNuke.Entities.Users.UserController.GetCurrentUserInfo();

                    ddlRoles.DataSource = rcc.GetRolesByGroup(PortalId, 0);
                    ddlRoles.DataTextField = "RoleName";
                    ddlRoles.DataValueField = "RoleID";
                    ddlRoles.DataBind();

                    ddlNodes.DataSource = mwHierarchyController.GetHierarchy();
                    ddlNodes.DataTextField = "Name";
                    ddlNodes.DataValueField = "NodeID";
                    ddlNodes.DataBind();                   
                    

                    //check if we have an ID passed in via a querystring parameter, if so, load that item to edit.
                    //ItemId is defined in the ItemModuleBase.cs file
                    //if (ItemId > 0)
                    //{
                    //    var tc = new ItemController();

                    //    var t = tc.GetItem(ItemId, ModuleId);
                    //    if (t != null)
                    //    {
                    //        txtName.Text = t.ItemName;
                    //        txtDescription.Text = t.ItemDescription;
                    //        ddlAssignedUser.Items.FindByValue(t.AssignedUserId.ToString()).Selected = true;
                    //    }
                    //}
                }
            }
            catch (Exception exc) //Module failed to load
            {
                Exceptions.ProcessModuleLoadException(this, exc);
            }
        }


        protected void btnSubmit_Click(object sender, EventArgs e)
        {
            //var t = new Item();
            //var tc = new ItemController();

            //if (ItemId > 0)
            //{
            //    t = tc.GetItem(ItemId, ModuleId);
            //    t.ItemName = txtName.Text.Trim();
            //    t.ItemDescription = txtDescription.Text.Trim();
            //    t.LastModifiedByUserId = UserId;
            //    t.LastModifiedOnDate = DateTime.Now;
            //    t.AssignedUserId = Convert.ToInt32(ddlAssignedUser.SelectedValue);
            //}
            //else
            //{
            //    t = new Item()
            //    {
            //        AssignedUserId = Convert.ToInt32(ddlAssignedUser.SelectedValue),
            //        CreatedByUserId = UserId,
            //        CreatedOnDate = DateTime.Now,
            //        ItemName = txtName.Text.Trim(),
            //        ItemDescription = txtDescription.Text.Trim(),

            //    };
            //}

            var nodeRelationship = new mwHierarchyRelations()
            {
                ID = Null.NullInteger,
                UserID = Convert.ToInt32(ddlUsers.SelectedValue),
                RoleID = Convert.ToInt32(ddlRoles.SelectedValue),
                HNodeID = Convert.ToInt32(ddlNodes.SelectedValue),
                FromDate = (DateTime?)dpFromDate.SelectedDate,
                ToDate = (DateTime?)dpToDate.SelectedDate,

                CreatedByUserId = UserId,
                CreatedOnDate = DateTime.Now,
                LastModifiedOnDate = DateTime.Now,
                LastModifiedByUserId = UserId
            };
                                
            mwHierarchyRelationsController.AddNodeRelation(nodeRelationship);

            //t.LastModifiedOnDate = DateTime.Now;
            //t.LastModifiedByUserId = UserId;
            //t.ModuleId = ModuleId;

            //if (t.ItemId > 0)
            //{
            //    tc.UpdateItem(t);
            //}
            //else
            //{

            //    tc.CreateItem(t);
            //}

            Response.Redirect(Common.Globals.NavigateURL());

        }

        protected void btnCancel_Click(object sender, EventArgs e)
        {
            Response.Redirect(Common.Globals.NavigateURL());
        }

    }


}